package com.ryl.yomall.canal.config;

import com.alibaba.otter.canal.protocol.CanalEntry;
import com.ryl.yomall.model.cart.CartInfo;
import com.ryl.yomall.model.common.constant.RedisConst;
import com.xpand.starter.canal.annotation.CanalEventListener;
import com.xpand.starter.canal.annotation.DeleteListenPoint;
import com.xpand.starter.canal.annotation.ListenPoint;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;

import java.math.BigDecimal;


@CanalEventListener
public class MyEventListener {
    @Autowired
    private RedisTemplate redisTemplate;

  /*  @InsertListenPoint
    public void onEvent(CanalEntry.EventType eventType, CanalEntry.RowData rowData) {
        rowData.getAfterColumnsList().forEach((c) -> System.err.println("By--Annotation: " + c.getName() + " ::   " + c.getValue()));
    }

    @UpdateListenPoint
    public void onEvent1(CanalEntry.RowData rowData) {
        System.err.println("UpdateListenPoint");
        rowData.getAfterColumnsList().forEach((c) -> System.err.println("By--Annotation: " + c.getName() + " ::   " + c.getValue()));
    }

    @DeleteListenPoint(destination = "example", schema = "yomall_order", table = {"cart_info"}

    )
    public void onEvent3(CanalEntry.EventType eventType,CanalEntry.RowData rowData) {
        System.err.println("DeleteListenPoint");

    }
    */

    @ListenPoint(destination = "example", schema = "yomall_order", table = {"cart_info"},
    eventType ={
    CanalEntry.EventType.UPDATE,
    CanalEntry.EventType.INSERT,
    CanalEntry.EventType.DELETE}
   )
    public void onEvent4(CanalEntry.EventType eventType, CanalEntry.RowData rowData) {

        CartInfo cartInfo = new CartInfo();

        if(eventType == CanalEntry.EventType.DELETE){

            rowData.getBeforeColumnsList ().forEach( c -> {
                System.err.println("Key: " + c.getName() + " ::  Value:: " + c.getValue());

    /*        try {
                cartInfo.getClass().getDeclaredField(c.getName()).set(c.getName(),c.getValue());
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            }*/
                if ("sku_id".equals(c.getName())) {
                    cartInfo.setSkuId (Long.parseLong(c.getValue()));
                }
            });
            System.out.println("delete-num:" + cartInfo.getSkuNum());
            //应该怎么处理
            redisTemplate.opsForHash().delete(RedisConst.USER_KEY_PREFIX +
                            cartInfo.getUserId() + RedisConst.USER_CART_KEY_SUFFIX,
                    cartInfo.getSkuId().toString());
        }else{
            rowData.getAfterColumnsList().forEach((c) -> {
                System.err.println("Key: " + c.getName() + " ::  Value:: " + c.getValue());

                if ("id".equals(c.getName())) {
                    cartInfo.setId(Long.parseLong(c.getValue()));
                } else if ("user_id".equals(c.getName())) {
                    cartInfo.setUserId(c.getValue());
                } else if ("sku_id".equals(c.getName())) {
                    cartInfo.setSkuId(Long.parseLong(c.getValue()));
                } else if ("cart_price".equals(c.getName())) {
                    cartInfo.setCartPrice(new BigDecimal (c.getValue()));
                } else if ("sku_num".equals(c.getName())) {
                    cartInfo.setSkuNum(Integer.parseInt(c.getValue()));
                } else if ("img_url".equals(c.getName())) {
                    cartInfo.setImgUrl(c.getValue());
                } else if ("sku_name".equals(c.getName())) {
                    cartInfo.setSkuName(c.getValue());
                } else if ("is_checked".equals(c.getName())) {
                    cartInfo.setIsChecked(Integer.parseInt(c.getValue()));
                }
            });
            System.out.println("insert or update num:" + cartInfo.getSkuNum());
            redisTemplate.opsForHash().put( RedisConst.USER_KEY_PREFIX +
                            cartInfo.getUserId() + RedisConst.USER_CART_KEY_SUFFIX,
                    cartInfo.getSkuId().toString(),cartInfo);
        }





    }
}
